{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "from aip import AipOcr , AipNlp ,AipSpeech"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\" 你的 APPID AK SK \"\"\"\n",
    "APP_ID = '  '\n",
    "API_KEY = '  '\n",
    "SECRET_KEY = '  '\n",
    "\n",
    "client = AipOcr(APP_ID, API_KEY, SECRET_KEY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\" 读取图片 \"\"\"\n",
    "def get_file_content(filePath):\n",
    "    with open(filePath, 'rb') as fp:\n",
    "        return fp.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "image = get_file_content('C:/Users/Administrator/anaconda文件夹/API/Baiduyun/img/book_1.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\" 如果有可选参数 \"\"\"\n",
    "options = {}\n",
    "options[\"language_type\"] = \"CHN_ENG\"\n",
    "options[\"detect_direction\"] = \"true\"\n",
    "options[\"detect_language\"] = \"true\"\n",
    "options[\"probability\"] = \"true\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'direction': 1,\n",
       " 'language': 3,\n",
       " 'log_id': 5149124712141095463,\n",
       " 'words_result': [{'probability': {'average': 0.974668,\n",
       "    'min': 0.929014,\n",
       "    'variance': 0.000523},\n",
       "   'words': 'awrence A Machi, Brenda T Mcevoy'},\n",
       "  {'probability': {'average': 0.940457, 'min': 0.853223, 'variance': 0.002226},\n",
       "   'words': 'The Literature Review: Six Steps to Snccess'},\n",
       "  {'probability': {'average': 0.876247, 'min': 0.626181, 'variance': 0.011341},\n",
       "   'words': 'English Language edition published by Corwin Press, a SAGE Publications Company of Thoushand Oass'},\n",
       "  {'probability': {'average': 0.975326, 'min': 0.933735, 'variance': 0.000465},\n",
       "   'words': 'London, New Delhi, Singapore and Washington D.C., 2009 by Corwin Press, Inc'},\n",
       "  {'probability': {'average': 0.943105, 'min': 0.462722, 'variance': 0.01929},\n",
       "   'words': '本书中文简体字翩译版由上海敦育出版社出版,限在中国大陆销售,末经出版者预先书面许可,不得以任何'},\n",
       "  {'probability': {'average': 0.995193, 'min': 0.939577, 'variance': 0.000224},\n",
       "   'words': '方式复制或发行本书的任何部分。'},\n",
       "  {'probability': {'average': 0.983874, 'min': 0.707418, 'variance': 0.003101},\n",
       "   'words': '上海市版权局著作权合同登记号图字09-2009362号'},\n",
       "  {'probability': {'average': 0.980274, 'min': 0.849373, 'variance': 0.001763},\n",
       "   'words': '图书在版编目(CIP)数据'},\n",
       "  {'probability': {'average': 0.923156, 'min': 0.381741, 'variance': 0.023507},\n",
       "   'words': '怎样做文献综述一一六步走向成功/「美]马奇( Machi. L.A.),'},\n",
       "  {'probability': {'average': 0.942788, 'min': 0.526299, 'variance': 0.01468},\n",
       "   'words': '「美]麦克伊沃( Mcevoy,B.T)著;陈静,肖思汉译'},\n",
       "  {'probability': {'average': 0.996157, 'min': 0.957606, 'variance': 0.000104},\n",
       "   'words': '上海:上海教育出版社,2011.6(2012.4重印)'},\n",
       "  {'probability': {'average': 0.984384, 'min': 0.822868, 'variance': 0.001927},\n",
       "   'words': '(象牙塔之旅·社会科学研究指导丛书)'},\n",
       "  {'probability': {'average': 0.968934, 'min': 0.63408, 'variance': 0.006339},\n",
       "   'words': 'ISBN978-7-5444-3037-1'},\n",
       "  {'probability': {'average': 0.923185, 'min': 0.490995, 'variance': 0.01808},\n",
       "   'words': 'I.①怎…Ⅱ.①马…②麦…③陈…④肖…Ⅲ.①论文一写作'},\n",
       "  {'probability': {'average': 0.897304, 'min': 0.350321, 'variance': 0.041106},\n",
       "   'words': 'N.①H152.3'},\n",
       "  {'probability': {'average': 0.975081, 'min': 0.39356, 'variance': 0.013047},\n",
       "   'words': '中国版本图书馆CIP数据核字(2011第073262号'},\n",
       "  {'probability': {'average': 0.973031, 'min': 0.676616, 'variance': 0.006147},\n",
       "   'words': '象牙塔之旅。社会科学研究指导丛书'},\n",
       "  {'probability': {'average': 0.960494, 'min': 0.689202, 'variance': 0.006507},\n",
       "   'words': '怎样做文献综述一一六步走向成功'},\n",
       "  {'probability': {'average': 0.937411, 'min': 0.352945, 'variance': 0.023819},\n",
       "   'words': '[美]劳伦斯・马奇布伦达·麦克伊沃著'},\n",
       "  {'probability': {'average': 0.985423, 'min': 0.92622, 'variance': 0.000705},\n",
       "   'words': '陈静肖思汉译'},\n",
       "  {'probability': {'average': 0.982632, 'min': 0.927252, 'variance': 0.000624},\n",
       "   'words': '高惠蓉张瑜审校'},\n",
       "  {'probability': {'average': 0.999729, 'min': 0.998509, 'variance': 0.0},\n",
       "   'words': '出版发行上海世纪出版股份有限公司'},\n",
       "  {'probability': {'average': 0.999451, 'min': 0.998335, 'variance': 0.0},\n",
       "   'words': '上海教育出版社'},\n",
       "  {'probability': {'average': 0.918768, 'min': 0.654093, 'variance': 0.01301},\n",
       "   'words': '易文网www.ewen.cc'},\n",
       "  {'probability': {'average': 0.996583, 'min': 0.967112, 'variance': 8.7e-05},\n",
       "   'words': '地址上海永福路123号'},\n",
       "  {'probability': {'average': 0.943434, 'min': 0.592005, 'variance': 0.017788},\n",
       "   'words': '邮编200031'},\n",
       "  {'probability': {'average': 0.943105, 'min': 0.721053, 'variance': 0.012331},\n",
       "   'words': '经销各地告'},\n",
       "  {'probability': {'average': 0.996129, 'min': 0.968893, 'variance': 7e-05},\n",
       "   'words': '印刷江苏启东市人民印刷有限公司'},\n",
       "  {'probability': {'average': 0.937811, 'min': 0.536897, 'variance': 0.022449},\n",
       "   'words': '开本700×10001/16印张8.25插页2'},\n",
       "  {'probability': {'average': 0.999724, 'min': 0.998474, 'variance': 0.0},\n",
       "   'words': '版次2011年6月第1版'},\n",
       "  {'probability': {'average': 0.998205, 'min': 0.977278, 'variance': 3.4e-05},\n",
       "   'words': '印次2012年4月第3次印刷'},\n",
       "  {'probability': {'average': 0.962606, 'min': 0.640619, 'variance': 0.010312},\n",
       "   'words': '书号ISBN978-7-5444-3037-1/G·2342'},\n",
       "  {'probability': {'average': 0.993175, 'min': 0.952173, 'variance': 0.000242},\n",
       "   'words': '定价18.00元'},\n",
       "  {'probability': {'average': 0.993598, 'min': 0.915063, 'variance': 0.000368},\n",
       "   'words': '(如发现质量问题,读者可向工厂调换)'}],\n",
       " 'words_result_num': 34}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\" 带参数调用通用文字识别, 图片参数为本地图片 \"\"\"\n",
    "client.basicGeneral(image, options)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 评论分析\n",
    "\n",
    "\n",
    "参数|是否必须|类型|说明\n",
    ":-|:-|:-|:-\n",
    "text\t|是|\tstring\t|输入的文本内容\n",
    "items\t|是|\tarray\t|输入的词列表\n",
    "+sentiment\t|是|\tnumber\t|表示情感极性分类结果, 0:负向，1:中性，2:正向\n",
    "+confidence\t|是|\tnumber\t|表示分类的置信度\n",
    "+positive_prob\t|是|\tnumber\t|表示属于积极类别的概率\n",
    "+negative_prob\t|是|\tnumber\t|表示属于消极类别的概率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\" 你的 APPID AK SK \"\"\"\n",
    "APP_ID = '  '\n",
    "API_KEY = '  '\n",
    "SECRET_KEY = '  '\n",
    "\n",
    "client = AipNlp(APP_ID, API_KEY, SECRET_KEY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "text = \"《将夜》里有好多我喜欢的桥段，可惜只能回答一次。 就是它吧。凭什么他们好活，让我去死。另：大爱君陌，真君子也。\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'items': [{'confidence': 0.896197,\n",
       "   'negative_prob': 0.0467115,\n",
       "   'positive_prob': 0.953288,\n",
       "   'sentiment': 2}],\n",
       " 'log_id': 6877766198485744968,\n",
       " 'text': '《将夜》里有好多我喜欢的桥段，可惜只能回答一次。 就是它吧。凭什么他们好活，让我去死。另：大爱君陌，真君子也。'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\" 调用词法分析 \"\"\"\n",
    "client.sentimentClassify(text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 语言合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\" 你的 APPID AK SK \"\"\"\n",
    "APP_ID = ''\n",
    "API_KEY = ''\n",
    "SECRET_KEY = ''\n",
    "\n",
    "client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "result  = client.synthesis('《将夜》里有好多我喜欢的桥段，可惜只能回答一次。 就是它吧。凭什么他们好活，让我去死。另：大爱君陌，真君子也。', 'zh', 1, {'vol': 5,})\n",
    "\n",
    "# 识别正确返回语音二进制 错误则返回dict 参照下面错误码\n",
    "if not isinstance(result, dict):\n",
    "    with open('auido.mp3', 'wb') as f:\n",
    "        f.write(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
